package com.marvels.approve.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.manager.dto.FlowNodeDTO;
import com.marvels.approve.manager.service.IFlowNodeService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**流程节点
 * @author hejie
 */
@RestController
@RequestMapping("approve/flow_node/service")
public class FlowNodeController {
	@Autowired
	private IFlowNodeService flowNodeService ;
	@RequestMapping("nlist")
	@ResponseBody
	public MarvelsResultDTO nodeList(@RequestBody(required=false) FlowNodeDTO flowNodeDTO){
		try {
			List datas = this.flowNodeService.findFlowNode(flowNodeDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	@RequestMapping("add")
	@ResponseBody
	public MarvelsResultDTO addNodeList(@RequestBody FlowNodeDTO node){
		try {
			this.flowNodeService.saveFlowNode(node);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), node);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	@RequestMapping("modify")
	@ResponseBody
	public MarvelsResultDTO modifyNodeList(@RequestBody FlowNodeDTO node){
		try {
			this.flowNodeService.saveFlowNode(node);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), node);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
}
